﻿var data = {};
var systemInfo = {}; //系统信息
var leftArticles = []; //左边的文章列表
var notices = []; //公告
var hotArticles = []; //热门文章
var friendUrls = []; //友情链接
systemInfo.netName = "";
systemInfo.adminName = "";
systemInfo.adminInfo = "";
systemInfo.whereFrom = "";
systemInfo.adminImg = "";
data.leftArticles = leftArticles;
data.systemInfo = systemInfo;
data.notices = notices;
data.hotArticles = hotArticles;
data.friendUrls = friendUrls;

var appVue = new Vue({
	el: "#app",
	data: data,
	methods: {
		articleDetail: function(id) {
			location.href = "detail.html?id=" + id;
		},
		searchByTag: function(tag) {
			location.href = "article.html?tag=" + tag;
		}
	}
});

$(function() {
	//加载文章列表
	layui.flow.load({
		elem: '#div_article_buttom',
		isAuto: false,
		done: function(page, next) {
			getArticlePage(function(result) {
				var resultData = result.data;
				for(var i = 0; i < resultData.length; i++) {
					leftArticles.push(resultData[i]);
				};
				next("", page < result.totalPage);
			}, page, 5);
		}
	});
	//加载公告列表
	getNoticeList(function(result) {
		var resultData = result.data;
		for(var i = 0; i < resultData.length; i++) {
			notices.push(resultData[i]);
		};
		//播放公告
		setTimeout(function() {
			playAnnouncement(3000);
		}, 500);
	});
	//加载热门文章
	getArticlePage(function(result) {
		var resultData = result.data;
		for(var i = 0; i < resultData.length; i++) {
			hotArticles.push(resultData[i]);
		};
	}, 1, 5, null, 1);
	//获取网站信息
	getSizeInfo(function(result) {
		var resultData = result.data;
		systemInfo.netName = resultData.siteShortTitle;
		systemInfo.adminName = resultData.adminRealname;
		systemInfo.adminInfo = resultData.adminProfession;
		systemInfo.whereFrom = resultData.adminPosition;
		systemInfo.adminImg = resultData.adminImg;
		document.title = resultData.siteTitle;
	});
	//获取友情链接
	getFriendUrlList(function(result) {
		var resultData = result.data;
		for(var i = 0; i < resultData.length; i++) {
			friendUrls.push(resultData[i]);
		};
	});
});

/**
 * 播放公告
 * @param {Object} interval
 */
function playAnnouncement(interval) {
	var index = 0;
	var $announcement = $('.home-tips');

	//自动轮换
	setInterval(function() {
		index++; //下标更新

		if(index >= $announcement.length) {
			index = 0;
		}
		$announcement.eq(index).stop(true, true).fadeIn().siblings('span').fadeOut(); //下标对应的图片显示，同辈元素隐藏
	}, interval);
}